$(function () {
    $("#jqGrid").jqGrid({
        url: '../systreetest/list',
        datatype: 'json',
        loadonce: true,
        ExpandColClick: true,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', width: 50, key: true, hidden: true},
     /*       {label: '', name: 'createUser', index: 'CREATE_USER', width: 80},
            {label: '', name: 'lastUpdateUser', index: 'LAST_UPDATE_USER', width: 80},
            {label: '', name: 'iconClass', index: 'ICON_CLASS', width: 80},
            {label: '', name: 'isOpen', index: 'IS_OPEN', width: 80},*/
            {label: '', name: 'menuLevel', index: 'MENU_LEVEL', width: 80, hidden: true},
            {label: '', name: 'menuNo', index: 'MENU_NO', width: 80,sortable:false,hidden: true},
            {label: '', name: 'name', index: 'NAME', width: 80},
            {label: '', name: 'openType', index: 'OPEN_TYPE', width: 80,hidden: true},
            {label: '', name: 'parentId', index: 'PARENT_ID', width: 80,hidden: true},
            {label: '', name: 'parentName', index: 'PARENT_NAME', width: 80,hidden: true},
            {label: '', name: 'url', index: 'URL', width: 80},
            {label: '', name: 'interceptUrl', index: 'INTERCEPT_URL', width: 80},
            {label: '', name: 'remark', index: 'REMARK', width: 80},
            {label: '', name: 'userOrgId', index: 'USER_ORG_ID', width: 80,hidden: true},
            {label: '', name: 'userRoleId', index: 'USER_ROLE_ID', width: 80,hidden: true},
            {label: '', name: 'createDateTime', index: 'CREATE_DATE_TIME', width: 80,hidden: true},
            {label: '', name: 'lastUpdateTime', index: 'LAST_UPDATE_TIME', width: 80,hidden: true},
            {label: '', name: 'iconColor', index: 'ICON_COLOR', width: 80},
            {label: '', name: 'orderNum', index: 'ORDER_NUM', width: 80},
            {label: '', name: 'isLeaf', index: 'ISLEAF', width: 80,hidden: true},
            {label: '', name: 'exPanded', index: 'EXPANDED', width: 80,hidden: true},
            {label: '', name: 'loAded', index: 'LOADED', width: 80,hidden: true},
            {label: '', name: 'isDelete', index: 'IS_DELETE', width: 80,hidden: true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        //rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: '#jqGridPager',
        jsonReader: {
            root: 'dataRows',
            page: 'page.currPage',
            total: 'page.totalPage',
            records: 'page.totalCount'
        },
        treeReader: {
            parent_id_field: 'parentId',
            level_field: 'menuLevel',  // 树的等级根节点为0，一次递增
            leaf_field: 'isLeaf',  // 是否是子节点
            expanded_field: 'exPanded',   // 是否展开
            loaded: 'loAded', //是否异步加载
            icon_field: 'icon',
            sort_field: 'name',   //指定字段排序
        },
        treeGrid: true,
        ExpandColumn: 'openType',  // 指定某列用于展开表格

        treedatatype: 'json',
        treeGridModel: 'adjacency', //treeGrid模式，跟json元数据有关 ,adjacency/nested
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sysTreeTest: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.sysTreeTest = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.sysTreeTest.id == null ? "../systreetest/save" : "../systreetest/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.sysTreeTest),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../systreetest/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../systreetest/info/" + id, function (r) {
                vm.sysTreeTest = r.sysTreeTest;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});